package com.bss.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andy.basics.MessageHandler;
import com.andy.basics.Service;
import com.bss.model.Building;
import com.bss.service.BuildingService;

@Controller
@Scope("request")
public class BuildingController extends com.andy.basics.Controller<Building> {

	private static Logger logger = Logger.getLogger(BuildingController.class);

	@Autowired
	private BuildingService buildingService;

	@Override
	public Service<Building> getService() {
		return this.buildingService;
	}

	@Override
	public Building create() {
		return new Building();
	}

	@Override
	public String getModelName() {
		return "Building";
	}

	@Override
	protected boolean validate(final Building building,
			final MessageHandler handler) {
		List<Building> b = this.buildingService.find("name='"
				+ building.getName() + "'");
		BuildingController.logger.debug("Number of entries with building name "
				+ building.getName() + ": " + b.size());
		if (b.size() > 0) {
			handler.addMessage("Name",
					"Building with name: " + building.getName()
							+ " already exists ! ");
			return false;
		}
		return true;
	}

	public List<Building> getBuildings() {
		return this.buildingService.getBuildings();
	}

	@Override
	public String add() {
		getModel().setDate(getDate());
		return super.add();
	}
}
